<?php
class Materia extends AppModel {
	var $name = 'Materia';
	var $belongsTo = array(
		'TipoMateria'=>array(
				'className'=>'TipoMateria',
				'foreignKey'=>'tipo_materia_id'
			),
		);
	function generateCodigo(){
		$result = $this->query('select 
									COALESCE(cast(max(d.codigo) as integer)+1,1) as "Materia__codigo" 
								from expedientes.materias d 
								where d.activo = true');
		$result = $result[0];
		return str_repeat('0',4-strlen($result['Materia']['codigo'])).$result['Materia']['codigo'];			
	}	
}
?>